﻿/************************************************************************
* Copyright (c) 2018 All Rights Reserved.
* 命名空间：DavidHelper
* 文件名：HttpHelper
* 创建人：David Rodríguez
* 创建时间：2018/7/31 星期二 9:32:33
* 描述：
*************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DavidHelper
{
    public class HttpHelper
    {
        public static string Post(string url, string postData, CookieCollection cookie, string contentType = null)
        {
            HttpWebRequest req = null;
            try
            {
                req = WebRequest.Create(url) as HttpWebRequest;
                if (cookie != null)
                {
                    req.CookieContainer = new CookieContainer();
                    req.CookieContainer.Add(cookie);
                }

                if (contentType != null)
                {
                    req.ContentType = contentType;
                }

                var userAgent =
                    "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36";
                req.UserAgent = userAgent;
                req.Method = "POST";
                req.Referer = url;
                req.Timeout = 9000;
                var data = Encoding.Default.GetBytes(postData);
                req.ContentLength = data.Length;
                using (var outstream = req.GetRequestStream())
                {
                    outstream.Write(data, 0, data.Length);
                }

                //发送请求并获取相应回应数据
                var response = req.GetResponse() as HttpWebResponse;
                //发送Post请求
                string content = null;
                using (var instream = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(instream, Encoding.UTF8))
                    {
                        //返回结果网页（html）代码
                        content = sr.ReadToEnd();
                    }
                }

                return content;
            }
            catch (Exception e)
            {
                return e.Message;
            }
            finally
            {
                req?.Abort();
            }
        }

        public static string Get(string url, CookieCollection cookie, string contentType = null)
        {
            HttpWebRequest req = null;
            try
            {
                req = WebRequest.Create(url) as HttpWebRequest;
                if (cookie != null)
                {
                    req.CookieContainer = new CookieContainer();
                    req.CookieContainer.Add(cookie);
                }

                req.ContentType = contentType;
                var userAgent =
                    "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36";
                req.UserAgent = userAgent;
                req.Method = "GET";
                req.Referer = url;

                req.Timeout = 3000;
                var response = req.GetResponse() as HttpWebResponse;
                string content = null;
                using (var instream = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(instream, Encoding.UTF8))
                    {
                        content = sr.ReadToEnd();
                    }
                }

                return content;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                req?.Abort();
            }
        }
    }
}